(function() {
    angular.module('BlurAdmin.pages.UserlistM')
        .controller('UserlistMCtrl', UserlistMCtrl);

    function UserlistMCtrl($scope, $openModal, $myconfig, $http, $datepicker, tongxin) {

        $scope.open = $openModal; //创建实例化对象
        function fn(val) { //关闭模态框方法
            $scope.myopen.opt_close = val;
        }

        function fn2(val) { //销毁实力方法
            $scope.myopen.dismiss = val;
        }
        $scope.myopen = function(page, size, id) { //打开模态框
            tongxin.setUserInfo(id);
            $scope.open.newOpen(page, size, $scope, fn, 'static');
        }
        $scope.myopen2 = function(page, size) { //打开模态框 ,可关闭
            $scope.open.newOpen(page, size, $scope, fn);
        }


        //日期插件
        $scope.format = "yyyy/MM/dd";
        $scope.altInputFormats = ['yyyy-MM-dd-HH'];
        $scope.dat = {
            dat1: $datepicker.dataNew(),
            dat2: $datepicker.dataNew(),
            dat3: $datepicker.dataNew(),
            dat4: $datepicker.dataNew(),
        };

        $scope.dat.dat1.date_ = '';
        $scope.dat.dat2.date_ = '';
        $scope.dat.dat3.date_ = '';
        $scope.dat.dat4.date_ = '';

        $scope.tongzhi = function(val, url) {
            tongxin.setUserInfo(val);
            // $scope.myopen2('app/pages/UserlistM/user/userInfo/userInfo.html', 'lg');
            $scope.myopen2(url, 'lg');

        }

        //分页
        $scope.maxSize = 5;
        // $scope.bigTotalItems = 50;
        $scope.bigCurrentPage = 1;
        $scope.pageChanged = function(val) {
            var Page_num = val //选中的页面数
            $scope.search_opt.page = val;
            Search_list();
        };

        //获取用户列表
        $scope.search_opt = {
            limit: 10,
            page: 1,
            "username": "", //用户昵称
            "mobile": "", //用户电话
            "address": "", //用户地址
            "sex": "all", //用户性别 用户性别，0：男，1：女， all：全部
            "start_birthday": "", //用户生日的开始区间
            "end_birthday": "", //用户生日的结束时间
            "start_createtime": "", //用户创建的开始时间
            "end_createtime": "", //用户创建的截止时间
            "modality": 'all'
        }

        var search_opt = {};
        $scope.Search = function() { //点击搜索，进行条件赋值
            search_opt = {};
            var OO = JSON.stringify($scope.search_opt);
            var opt2 = JSON.parse(OO);
            //地址搜索
            var add_text = '';

            var add1 = $('#add1 option:selected').text();
            var add2 = $('#add2 option:selected').text();
            var add3 = $('#add3 option:selected').text();
            add1 && (add1 != '省') && (add_text += add1);
            add2 && (add2 != '市') && (add_text += ('-' + add2));
            add3 && (add3 != '区') && (add_text += ('-' + add3));
            //时间赋值

            var data1 = document.getElementById("dat1") ? document.getElementById("dat1").value : null;
            var data2 = document.getElementById("dat2") ? document.getElementById("dat2").value : null;
            var data3 = document.getElementById("dat3") ? document.getElementById("dat3").value : null;
            var data4 = document.getElementById("dat4") ? document.getElementById("dat4").value : null;

            opt2.start_birthday = data1;
            opt2.end_birthday = data2;
            opt2.start_createtime = data3;
            opt2.end_createtime = data4;

            opt2.username && (search_opt.username = opt2.username);
            opt2.mobile && (search_opt.mobile = opt2.mobile);
            add_text && (search_opt.address = add_text); //地址
            opt2.sex && (opt2.sex != 'all') && (search_opt.sex = opt2.sex);
            opt2.start_birthday && (search_opt.start_birthday = opt2.start_birthday);
            opt2.end_birthday && (search_opt.end_birthday = opt2.end_birthday);
            opt2.start_createtime && (search_opt.start_createtime = opt2.start_createtime);
            opt2.end_createtime && (search_opt.end_createtime = opt2.end_createtime);
            opt2.modality && (search_opt.modality = opt2.modality);
            if (opt2.modality == 'all') {
                delete opt2.modality
            }

            Search_count_b();
        }

        function Search_count_b() { //用户列表数据统计
            var OO = JSON.stringify(search_opt);
            var opt2 = JSON.parse(OO);
            if (opt2.modality == 'all') {
                delete opt2.modality;
            }
            $http.get($myconfig.api.users_users_count_b, { params: opt2 }).then(function(res) {
                $scope.bigTotalItems = res.data.data;
                Search_list();
            });
        }

        function Search_list() { //获取列表信息
            search_opt.limit = $scope.search_opt.limit;
            search_opt.page = $scope.search_opt.page;
            var OO = JSON.stringify(search_opt);
            var opt2 = JSON.parse(OO);
            if (opt2.modality == 'all') {
                delete opt2.modality;
            }
            $http.get($myconfig.api.users_users_list_b, { params: opt2 }).then(function(res) {
                if (res.data.success) {
                    $scope.Search_list_data = [];
                    for (var i = 0; i < res.data.data.length; i++) { //添加条数属性

                        res.data.data[i].num = (($scope.search_opt.page - 1) * $scope.search_opt.limit) + i + 1;
                        res.data.data[i].LastTime = {};
                        res.data.data[i].modality && (res.data.data[i].LastTime = res.data.data[i].modality[0]);
                        var o = 'assets/img/app/herd.png';
                        if (res.data.data[i].headimgurl) {
                            res.data.data[i].headimgurl = res.data.data[i].headimgurl.length > 3 ? ($myconfig.api.Qiniu_url + res.data.data[i].headimgurl) : (res.data.data[i].wxheadimgurl ? res.data.data[i].wxheadimgurl : o);
                        } else {
                            res.data.data[i].headimgurl = res.data.data[i].wxheadimgurl ? res.data.data[i].wxheadimgurl : o;
                        }

                        if (res.data.data[i].modality && res.data.data[i].modality[0] && res.data.data[i].modality[1]) {
                            var t1 = Date.parse(res.data.data[i].modality[0].lasttime);
                            var t2 = Date.parse(res.data.data[i].modality[1].lasttime);
                            res.data.data[i].LastTime.durations = res.data.data[i].modality[1].durations == "" ? res.data.data[i].modality[0] : res.data.data[i].modality[1];
                            res.data.data[i].LastTime = (t2 - t1) >= 0 ? res.data.data[i].modality[1] : res.data.data[i].modality[0];
                            res.data.data[i].LastTime.durations == "" && (res.data.data[i].LastTime = {});
                        }
                    }
                    $scope.Search_list_data = res.data.data;
                }
            })


        }

        //查看用户地址信息
        $scope.point=function(opt){


            $scope.opt_ditu='asdasd';
            $scope.myopen2('app/pages/UserlistM/user/point/point.html', 'lg');

            console.log(opt);
            
            setTimeout(function() {
               adds(opt);
            }, 200);
            function adds(opt) {
            var ix = (!opt) ? [116.404, 31.915] : [opt.lng,opt.lat];
            var city = opt.city ? opt.city : '北京';
            var map = new BMap.Map("allmap"); // 创建Map实例
            map.centerAndZoom(new BMap.Point(ix), 11); // 初始化地图,设置中心点坐标和地图级别
            map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
            map.setCurrentCity(city); // 设置地图显示的城市 此项是必须设置的
            map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放

            // 百度地图API功能
            var point = new BMap.Point(ix[0], ix[1]);
            console.log(ix);

            map.centerAndZoom(point, 11);
            var marker = new BMap.Marker(point); // 创建标注
            map.addOverlay(marker); // 将标注添加到地图中
            marker.disableDragging(); // 不可拖拽
            // marker.enableDragging();

            // marker.addEventListener("dragend", overlay_style); //拖动事件

            function overlay_style(e) {
                var p = e.target;
                var geoc = new BMap.Geocoder();
                var pt = e.point
                if (p instanceof BMap.Marker) {
                    console.log(p.getPosition().lat, p.getPosition().lng);
                    $scope.revData.point.coordinate[0] = p.getPosition().lng;
                    $scope.revData.point.coordinate[1] = p.getPosition().lat;
                    console.log($scope.revData.point.coordinate);
                }

                // geoc.getLocation(pt, function(rs) {
                //     var addComp = rs.addressComponents;
                //     var dizhi = addComp.province + "-" + addComp.city + "-" + addComp.district;
                //     if (addComp.city == addComp.province) {
                //         var dizhi = addComp.city + "-" + addComp.district;
                //     }
                //     console.log(dizhi);
                //     $scope.revData.point.address = dizhi;
                //     $scope.revData.point.city = addComp.city;

                // });
            }
        }
        }

        $scope.Search();

    }
})();